// pages/activity/coinRank/exchange/index.js
const { ActivityModel } = require('../../../../utils/activity-model.js')
const activityModel = new ActivityModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: '',
    merchantId: '',
    cityCode: '',
    cityName: '',
    shopId: '',
    shopName: '',
    packageInfo: {},
    sorts: [],
    modalShow: false,
    canExchange: false,
    allShops: [],
    canSelectShopIds: [],
    beanAssets: 0
  },
  close() {
    this.setData({
      modalShow: false
    })
    this.getBeans()
  },
  selectShop() {
    if (this.data.canSelectShopIds.length) {
      wx.navigateTo({
        url: `/pages/currencyShop/chooseStore/index?shopId=${this.data.shopId}&cityCode=${this.data.cityCode}&cityName=${this.data.cityName}&from=activity&canSelectShopIds=${JSON.stringify(this.data.canSelectShopIds)}`
      })
    }
  },
  exchangeCoin(e) {
    const { item } = e.currentTarget.dataset
    if (this.data.canExchange && this.data.beanAssets >= item.ticket) {
      activityModel.goldRankExchange({
        shopId: this.data.shopId,
        activityId: this.data.activityId,
        orderNo: item.orderNo
      }).then(() => {
        this.setData({
          modalShow: true
        })
        activityModel.goldRankBean({
          activityId: this.data.activityId
        }).then(data => {
          const allShops = (data || []).sort((a, b) => b.amountAvail - a.amountAvail)
          this.setData({
            allShops
          })
          this.calcBeans()
        }).catch(err => {
          wx.showToast({
            title: `${err.error}`,
            icon: 'none'
          })
        })
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none'
        })
      })
    }
  },
  getDetail() {
    activityModel.goldDetail({
      activityId: this.data.activityId,
      merchantId: this.data.merchantId
    }).then(data => {
      const packageInfo = data.activityRule.packageInfo || {}
      const sorts = JSON.parse(JSON.stringify((packageInfo.packageDetails || [])))
      sorts.sort((a, b) => a.ticket - b.ticket)
      this.setData({
        packageInfo,
        sorts
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },
  calcBeans() {
    const allShops = this.data.allShops.filter(item => item.shopId === this.data.shopId)
    const myBean = (allShops[0] || {}).amountAvail || 0
    this.setData({
      beanAssets: myBean
    })
  },
  getBeans() {
    activityModel.goldRankLimit({
      activityId: this.data.activityId
    }).then(data => {
      this.setData({
        canExchange: data
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },
  getList() {
    activityModel.goldRankBean({
      activityId: this.data.activityId
    }).then(data => {
      const allShops = (data || []).sort((a, b) => b.amountAvail - a.amountAvail)
      const myShop = allShops[0] || {}
      this.setData({
        allShops,
        cityCode: myShop.cityCode || '',
        cityName: myShop.cityName || '',
        shopId: myShop.shopId || '',
        shopName: myShop.shopName || '',
        canSelectShopIds: (data || []).map(item => item.shopId)
      })
      this.calcBeans()
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      activityId: options.activityId,
      merchantId: options.merchantId
    })
    this.getList()
    this.getBeans()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDetail()
  }
})